package com.sinry.middleware.messages.producer;

import com.sinry.message.enums.RabbitQueue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Optional;

/**
 * @author linxinlei
 * @project sinry-cloud
 * @description
 * @date 2022/8/8
 **/
@Slf4j
@Component
public class MessageHandler {

    @Resource
    ApplicationContext context;

    public Optional<MessageProducer> with(RabbitQueue rabbitQueue) {
        Map<String, MessageProducer> beansOfType = context.getBeansOfType(MessageProducer.class);
        return beansOfType.values()
                .stream()
                .filter(value -> value.inQueue(rabbitQueue))
                .findFirst();
    }

}
